﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace Subattle
{
    public partial class Page : UserControl
    {
        List<UIElement> pingedObjects = new List<UIElement>();
        double pingTimer = pingInterval;
        Vector2 pingDirection;
        Point pingOrigin = new Point(0, 0);
        const double pingInterval = 2;
        const double pingExpandTime = 1.0;
        const double pingMaxRadius = 250;

        private double GetCurrentPingRadius() { return Math.Min(1, pingTimer / pingExpandTime) * pingMaxRadius; }

        private void UpdatePing(TimeSpan elapsed)
        {
            // Update ping timer
            pingTimer += elapsed.TotalSeconds;

            // if we've reached the interval 
            if (pingTimer >= pingInterval)
            {
                // Wrap around
                pingTimer %= pingInterval;

                // Play ping sound
                soundPing.Stop(); // stop if already playing
                soundPing.Play();

                // Set origin to sub's position
                pingOrigin = new Point(Sub.Position.X, Sub.Position.Y);

                // Set pingwave's angle
                pingDirection = Sub.Direction;
                ((RotateTransform)pingWave.RenderTransform).Angle = Sub.Angle;

                pingedObjects.Clear();
            }


            // get a double representing the amount our ping wave has expanded
            double expansion = pingTimer / pingExpandTime;

            // radius of the ping is
            double outerRadius = Math.Min(1, expansion);
            double innerRadius = MathHelper.Clamp(expansion - 0.1, 0.9, 1);

            // Fade out as the wave reaches end of expansion
            pingWave.Opacity = 1 - outerRadius;

            // if innerRadius == 1 then it should dissapear
            if (innerRadius >= 1)
            {
                pingWave.Visibility = Visibility.Collapsed;
            }
            else if (outerRadius > 0.1 && innerRadius < 1)
            {
                // Set visible
                pingWave.Visibility = Visibility.Visible;

                // Set PingWave Size
                pingWave.Height = pingWave.Width = Math.Max(2, outerRadius * pingMaxRadius * 2);

                // Set pingwave's rotation center
                ((RotateTransform)pingWave.RenderTransform).CenterX = ((RotateTransform)pingWave.RenderTransform).CenterY = outerRadius * pingMaxRadius;

                // Set pingwave's radial gradient to achieve a cool effect :) 
                // you can outcomment this if you want to play around alittle, it was a little too slow :[
                //PingGradient.GradientStops[0].Offset = innerRadius;

                // Set ping position
                Canvas.SetLeft(pingWave, pingOrigin.X - outerRadius * pingMaxRadius);
                Canvas.SetTop(pingWave, pingOrigin.Y - outerRadius * pingMaxRadius);
            }

            Vector2 pingStart = new Vector2((float)pingOrigin.X, (float)pingOrigin.Y);

            // Check we hit something with our sonar
            if (!pingedObjects.Contains(Enemy))
            {
                Enemy.Opacity = 0;
                Vector2 enemyDirection = Enemy.Position - pingStart;
                double pingRadius = GetCurrentPingRadius();
                float enemyDistance = enemyDirection.Length();

                if (enemyDistance < pingRadius && enemyDistance >= Math.Max(pingRadius - 20,0) && Vector2.Dot(Enemy.Position - Sub.Position, pingDirection) > 0)
                {
                    pingedObjects.Add(Enemy);
                    Enemy.Opacity = 1;
                    //PlaySound("response.mp3");
                    soundResponse.Stop();
                    soundResponse.Play();
                }
            }
            else
            {
                // Fade the enemy out...
                Enemy.Opacity = MathHelper.Clamp(Enemy.Opacity - elapsed.TotalSeconds * 1.5, 0, 1);
            }

            foreach (var torpedo in torpedoHandler.Torpedos)
            {
                if (torpedo.Owner == Sub)
                    continue;

                if (!pingedObjects.Contains(torpedo))
                {
                    torpedo.Opacity = 0;
                    Vector2 torpedoDirection = torpedo.Position - pingStart;
                    double pingRadius = GetCurrentPingRadius();
                    float torpedoDistance = torpedoDirection.Length();
                    if (torpedoDistance < pingRadius && torpedoDistance >= Math.Max(pingRadius - 20, 0) && Vector2.Dot(torpedo.Position - Sub.Position, pingDirection) > 0)
                    {
                        pingedObjects.Add(torpedo);
                        torpedo.Opacity = 1;
                        //PlaySound("response.mp3");
                        soundResponse.Stop();
                        soundResponse.Play();
                    }
                }
                else
                {
                    torpedo.Opacity = Math.Max(0, torpedo.Opacity - elapsed.TotalSeconds);
                }
            }

        }
    }
}
